var targetObj; //生成的目标target容器
var form;
var selectArr;
$(function () {
    form = $('#xh_form');
    getType();
    bindTypeSelect();//点击型号
    bindDestinationSelect();//点击目的地

    function bindDestinationSelect() {
        $('#iDestination').unbind('change').on('change',function () { //类型点击事件
            var text =$(this).find("option:selected").text();
            var str ='';
            if(targetObj != null){
                targetObj.remove();
            }
            if(text === '公司'){
                str = '<tr><td colspan="4"><a id="save" class="btn btn-default pull-right" style="margin-right: 20px">提交</a></td>' +
                    '</tr>';
                targetObj  = $(str);
                form.find('table tbody').append(targetObj);
            }else{
                str = '<tr><td><label>快递公司:</label></td><td colspan="3"><input id="expressCompany" name="expressCompany" class="form-control" placeholder="如申通、顺丰等等，必填"/></td></tr>';
                str += '<tr><td><label>快递单号:</label></td><td colspan="3"><input id="expressSend" name="expressSend" class="form-control" placeholder="必填"/></td></tr>';
                str += '<tr><td colspan="4"><a id="save" class="btn btn-default pull-right" style="margin-right: 20px">提交</a></td>' +
                    '</tr>';
                targetObj  = $(str);
                form.find('#sure').remove();
                form.find('table tbody').append(targetObj);
            }
        });
    }

    function bindTypeSelect() {
        $('#sModel').unbind('change').on('change',function () { //类型点击事件
            //获取商品填入
            getProd($(this).val());
        });
    }

    function getType() {
        $.ajax({
            url:baseUrl+'device/getTabletData',
            method:'post',
            data:{model:$('#sModel_data').val()},
            dataType:'json',
            success:function (data) {
                fillSelect('sModel',data.model);
                document.getElementById("sBrand").value=data.Imei;
            }
        });
    }

    function getProd(id,$value) {
        $.ajax({
            url:baseUrl+'device/getBrandData',
            method:'post',
            data:{model:id},
            dataType:'json',
            success:function (data) {
                document.getElementById("sBrand").value=data;
            }
        });
    }

    function fillSelect(selector,data,id,text) {
        var str = '<option value="" style="display: none" selected>请选择</option>';
        if(id != null && text != null){
            $.each(data,function (key,val) {
                str += '<option value="'+val[id]+'">'+val[text]+'</option>';
            })
        } else {
            $.each(data,function (key,val) {
                if (val === $('#sModel_data').val()){
                    str += '<option value="'+key+'" selected>'+val+'</option>';
                }else{
                    str += '<option value="'+key+'">'+val+'</option>';
                }
            })
        }
        $('#'+selector).empty().append(str);
    }

    form.on('click','#save',function () {
        var userId = $('#id_data').val();
        var logonName = $('#sLogonName_data').val();
        var userName = $('#sUserName_data').val();
        var sImei = $('#sImei').val();
        new xhForm(form).submit(baseUrl + 'device/saveRepairData',{userId:userId, logonName:logonName, userName:userName,sImei:sImei},function (res) {
            if(res.code === 200){
                window.top.swal('成功',res.msg,'success');
                window.top.xhTop.closeModal();
            }
        });
    });
});

function setEnable() {
    selectArr = [];
    form.find('select,input').each(function () {
        if($(this).attr('disabled') != undefined){
            $(this).attr('disabled',false);
            selectArr.push($(this));
        }
    });
}
